// pages/index/code.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:0,
    time:60,
    getCode:'获取验证码',
    disabled:'false',
    yanzhengma:'',
    phone:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
        this.setData({
          phone:options.phone
        })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
     this.time1()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
 
 

  // 获取验证码
  change(e){
    var value = e.detail.value;
    if(value != ''){
      this.setData({
        type:1,
        yanzhengma:e.detail.value
      })
    }else{
      this.setData({
        type:0,
      })
    }
  },

  //倒计时
  time(e){
    var disabled = e.currentTarget.dataset.disabled 
    if(disabled == true){
      return false;
    }
    var that = this;
     var times = 60
     var i = setInterval(function() {
          times--
          if (times <= 0) {
            that.setData({
              color: "#ff6f10",
              disabled: false,
              getCode: "获取验证码",
         })
         clearInterval(i)
             
          } else {
              
            
           that.setData({
            getCode: "重新获取" + times + "s",
            color: "#999",
            disabled: true
       })
          }
     }, 1000)

  },
  //点击完成时跳转
   navcomplete(){
      let yanzhengma=wx.getStorageSync('yanzhengma2')
      if(this.data.yanzhengma==yanzhengma){
        wx.request({
           url:'https://zujieba.com/Api/member/phone.html',
           data:{id:wx.getStorageSync('uid'),phone:this.data.phone},
           method:'POST',
           success:(res)=>{
               console.log(res);
               if(res.data.success==1){
                 wx.showToast({
                   title: res.data.tips,
                   icon:'success',
                   duration:2000,
                  
                   
                 })
                 wx.setStorageSync('phone', this.data.phone)
                 wx.navigateBack({
                  delta:2,
                  complete: (res) => {},
                })
                
               }else{
                 wx.showToast({
                   title: res.data.tips,
                   icon:'none'
                 })
               }
           }
        })
           
      
      }else{
        wx.showToast({
          title: '验证码输入错误',
        })
      }

   },
   time1(){
   
   
    var that = this;
     var times = 60
     var i = setInterval(function() {
          times--
          if (times <= 0) {
                
            that.setData({
              color: "#ff6f10",
              disabled: false,
              getCode: "获取验证码",
         })
         clearInterval(i)
             
          } else {
         
           that.setData({
            getCode: "重新获取" + times + "s",
            color: "#999",
            disabled: true
       })
          }
     }, 1000)

  },
})